package com.huslee.shark.alert;

import com.huslee.shark.alert.config.DingtalkConfigs;
import com.huslee.shark.alert.handler.DingtalkAlerter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;


/**
 * @Desc
 * @Author lishuai
 * @Create 2021/1/13
 */
@Component
@EnableScheduling
@EnableConfigurationProperties(DingtalkConfigs.class)
@ConditionalOnProperty(prefix = "basealert.dingtalk", name={"configs"}, matchIfMissing = true)
public class DingtalkAlert {
    private static Map<String, DingtalkAlerter> dingtalkAlerterMapping = new HashMap<>();

    @Autowired
    private DingtalkConfigs dingtalkConfigs;


    @PostConstruct
    private void initDingtalkAlert(){
       dingtalkConfigs.getConfigs().forEach(it->{
           DingtalkAlerter alerter = new DingtalkAlerter();
           alerter.setAsyn(dingtalkConfigs.getAsyn() == null ? false : dingtalkConfigs.getAsyn());
           alerter.setDingtalkConfig(it);
           alerter.setBlackUsers(dingtalkConfigs.getBlacklist());
           dingtalkAlerterMapping.put(it.getName(), alerter);
       });


    }

    public static DingtalkAlerter get(String name) {
        return dingtalkAlerterMapping.get(name);
    }

}
